package com.incar.chuxing.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.incar.chuxing.R;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.NetUtils;

public class SplashActivity extends AppCompatActivity {
    private Context mContext = this;
    private NetworkInfoReceiver networkInfoReceiver;
    private AlertDialog netWorkAlertDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isNetAvailable();
    }

    //检查网络连接状态
    private void isNetAvailable() {
        if (!NetUtils.isNetworkAvailable(mContext)) {
            Logger.i("网不可用");
            //没有网络，注册网络改变监听
            IntentFilter filter = new IntentFilter();
            filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            networkInfoReceiver = new NetworkInfoReceiver();
            registerReceiver(networkInfoReceiver, filter);
            netWorkAlertDialog = new AlertDialog.Builder(mContext).setTitle(getString(R.string.dialogTitle)).setMessage(getString(R.string.badNetwork))
                    .setPositiveButton("去设置", (
                            dialog, which) -> {
                        Intent intent = new Intent(Settings.ACTION_SETTINGS);
                        startActivity(intent);
                    })
                    .setNegativeButton("取消", (dialog, which) -> {
                        if (networkInfoReceiver != null) {
                            unregisterReceiver(networkInfoReceiver);
                        }
                        finish();
                        AppManager.getAppManager().AppExit(mContext);
                    }).setCancelable(false).show();
        } else {
            //网络正常的处理
            startActivity(new Intent(mContext, MainActivity.class));
            finish();
        }
    }

    //网络改变 广播接收
    private class NetworkInfoReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                if (NetUtils.isNetworkAvailable(context)) {
                    if (networkInfoReceiver != null) {
                        unregisterReceiver(networkInfoReceiver);
                        if (netWorkAlertDialog != null) {
                            netWorkAlertDialog.dismiss();
                        }
                    }

                    if (!isDestroyed()) {
                        //处理网络恢复后
                        startActivity(new Intent(mContext, MainActivity.class));
                        finish();
                    }
                }
            }
        }
    }


}
